/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getLen(ListNode* head){
        
        stack<ListNode*> st;
        while(head){
            st.push(head);
            head=head->next;
        }
        
        return st.size();
    }
    void delNode(ListNode* head,int pos){

        ListNode* cur=head;
        ListNode* pre=nullptr;
        while(pos--){
            pre=cur;
            cur=cur->next;
        }
        pre->next=cur->next;
        delete cur;
        cur=nullptr; 
    }
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        
        int len=getLen(head);
        ListNode dummy;
        dummy.next=head;
        delNode(&dummy,len-n+1);

        return dummy.next;
    }
};
